package com.example.administrator.nurse.util;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.administrator.nurse.model.CommonViewHolder;
import com.example.administrator.nurse.model.RecyclerViewCommonAdapter;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jcodecraeer.xrecyclerview.progressindicator.DivierItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 *  Created by 林冠宏 on 2016/4/23.
 *  xRecyclerView 工具类
 *  默认采用 线性布局
 */

public class recyclerViewHelper<T> {

    public List<T> list =new ArrayList<>();
    public RecyclerViewCommonAdapter adapter; /** 适配器 */
    private Context context;
    private XRecyclerView recyclerView;

    public recyclerViewHelper(Context context, XRecyclerView recyclerView){
        this.context = context;
        this.recyclerView = recyclerView;
    }

    public XRecyclerView getRecyclerView(){
        return recyclerView;
    }

    private RecyclerView.LayoutManager layoutManager = null;
    public recyclerViewHelper withLayoutManager(RecyclerView.LayoutManager layoutManager){
        this.layoutManager = layoutManager;
        return this;
    }

    private boolean isPullRefrshOpen = false; /** 默认不开启下拉刷新 */
    public recyclerViewHelper withPullRefrshOpen(boolean isPullRefrshOpen){
        this.isPullRefrshOpen = isPullRefrshOpen;
        return this;
    }

    public recyclerViewHelper withAdapter(RecyclerViewCommonAdapter adapter){
        this.adapter = adapter;
        return this;
    }

    private int adapterLayout;
    public recyclerViewHelper withAdapter(int adapterLayout){
        this.adapterLayout = adapterLayout;
        return this;
    }

    /** 接口分离，下拉刷新自己添加 */
    public interface loadMoreListener{
        void loadMore();
    }

    /** 下拉刷新 */
    public interface refreshListener{
        void refresh();
    }
    private refreshListener refreshListener;
    public recyclerViewHelper withRefresh(refreshListener refreshListener){
        this.refreshListener = refreshListener;
        return this;
    }

    private loadMoreListener loadMoreListener;
    public recyclerViewHelper withLoadMore(loadMoreListener loadMoreListener){
        this.loadMoreListener = loadMoreListener;
        return this;
    }

    public recyclerViewHelper initRecyclerView() {
        initAdapter();
        recyclerView.setLayoutManager(layoutManager == null ? new LinearLayoutManager(context) : layoutManager);

        /** 这两个不建议改 */
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        recyclerView.setPullRefreshEnabled(isPullRefrshOpen); /** 不需要下拉刷新 */
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if(refreshListener!=null){
                    refreshListener.refresh();
                }
            }

            @Override
            public void onLoadMore() {
                if(loadMoreListener!=null){
                    loadMoreListener.loadMore();
                }
            }
        });
        if(adapter!=null){
            recyclerView.setAdapter(adapter);
        }
        return this;
    }

    public interface BindViewListener{
        void bindView(CommonViewHolder holder, final int position);
    }

    private BindViewListener bindViewListener;
    public recyclerViewHelper withBindView(BindViewListener bindViewListener){
        this.bindViewListener = bindViewListener;
        return this;
    }

    public interface TestDataInterface<T>{
        T addTestData(int i);
    }
    private TestDataInterface testDataInterface;
    public recyclerViewHelper withTestData(TestDataInterface testDataInterface){
        this.testDataInterface = testDataInterface;
        return this;
    }
    private void initAdapter(){
        /** 测试数据 */
        for(int i=0;i<20;i++){
            if(testDataInterface!=null){
                list.add((T)testDataInterface.addTestData(i));
            }
        }
        if(adapter== null){
            adapter = new RecyclerViewCommonAdapter(context,list) {

                @Override
                protected int getLayoutId() {
                    return adapterLayout;
                }

                @Override
                protected void bindView(CommonViewHolder holder, final int position) {

                    if(bindViewListener!=null){
                        bindViewListener.bindView(holder,position);
                    }
                }
            };
        }
    }
    public void addItemDecoration(int orientation){
        recyclerView.addItemDecoration(new DivierItemDecoration(context, orientation));
    }

}
